// pages/my_account/index.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sessionId:'',
        userInfo: {
            nickName: '未登录',
            avatarUrl: '/images/icon-avatar.png',
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        wx.showTabBar();

        var that = this;

        var sessionId = wx.getStorageSync('sessionId')
        if (sessionId) {
            this.setData({
                sessionId: sessionId,
                ["userInfo.nickName"]: app.globalData.userInfo.nickName,
                ["userInfo.avatarUrl"]: app.globalData.userInfo.avatarUrl,
            });
        }
        
        app.WxRequest.getRequest('/wx/userinfo')
            .then(res => {
                if (res.data.code != '-1') {
                    that.setData({
                        ["userInfo.balance"]: res.data.balance,
                    });
                }
            })

        // wx.request({
        //     //后台接口地址
        //     url: app.server.addr + '/wx/userinfo',
        //     method: 'GET',
        //     header: {
        //         'content-type': 'application/json',
        //         'sessionId':wx.getStorageSync('sessionId')
        //     },
        //     success: function (res) {
        //         console.log("调用后台用户信息接口，返回的数据 = %o", res)
        //         if(!res.data.code){
        //             that.setData({
        //                 ["userInfo.balance"]: res.data.balance,
        //             });
        //         }
        //     },
        //     fail: function (res) {
        //         console.log(res);

        //     },
        //     complete: function () {
        //         wx.hideLoading()
        //     }
        // })
    },
    onShow: function(){
        this.loadUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    logout() {
        // 用户退出
        wx.removeStorageSync("sessionId");
        wx.removeStorageSync("userInfo");
        wx.showToast({
            title: '已退出登录',
            mask: true,
            success: function() {
                setTimeout(function() {
                    wx.navigateBack();
                }, 1500);
            }
        })
    },
    handleinfo(e) {
        console.log(e);
    },
    handlephone(e) {
        console.log(e);
    },
    loadUserInfo:function(){
        var that = this;
        var sessionId = wx.getStorageSync('sessionId')
        if (sessionId) {
            that.setData({
                sessionId: sessionId,
                userInfo: app.globalData.userInfo
            });

            console.info("发起请求")
            app.WxRequest.getRequest('/wx/userinfo')
            .then(res => {
                if (res.data.code != '-1') {
                    that.setData({
                        // ["userInfo.balance"]: res.data.balance,
                    });
                }
            })

        } else {
            this.setData({
                sessionId: '',
                userInfo: {
                    nickName: '未登录',
                    avatarUrl: '/images/icon-avatar.png',
                }
            });
        }
    }
})